//
//  StuClockVC.m
//  test
//
//  Created by 朱圳 on 2018/3/20.
//  Copyright © 2018年 朱圳. All rights reserved.
//

#import "StuClockVC.h"
#import "PlanModel.h"
#import "ContentModel.h"
#import "ClockTableCell.h"
#import "YHClockhead.h"
#import "JWClockBottomView.h"
@interface StuClockVC ()<UITableViewDelegate,UITableViewDataSource,JWClockBottomViewDelegate>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)UIView *noDataView;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,strong)NSMutableArray *contentArray;
@property(nonatomic,strong)NSMutableArray *contentIDArray;
@property(nonatomic,strong)PlanModel *model;
@property(nonatomic,strong)NSArray *arrStatuses;
@property (nonatomic,assign)NSUInteger counts;
@property (nonatomic,strong)NSString *homework;
@property (nonatomic,strong)NSString *jwstate;

@end

@implementation StuClockVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setBack];
}
-(void)setBack{
    /*设置导航栏上面的内容*/
    UIButton *backBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    // 设置图片
    [backBtn setBackgroundImage:[UIImage imageNamed:@"navigationbar_back"] forState:UIControlStateNormal];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"navigationbar_back_highlighted"] forState:UIControlStateHighlighted];
    // 设置尺寸
    backBtn.size = backBtn.currentBackgroundImage.size;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];

}
-(void)viewWillAppear:(BOOL)animated{
        [super viewWillAppear:animated];
    /**
     解决自定义返回 手势失效
     */
    self.navigationController.interactivePopGestureRecognizer.delegate=(id)self;
    [self requestPlanData];
    self.title = @"处理打卡";
}
//请求具体打卡数据
-(void)requestPlanData{
    [self.dataArray removeAllObjects];
    [self.contentArray removeAllObjects];
    [self.view addSubview:self.tableView];
    __weak typeof(self) weakSelf = self;
    NSDictionary *dict = @{
                           @"jwid" : JWid,
                           @"jwpassword" : JWpassword,
                           @"days" : self.days,
                           @"sid" :self.sid
                           };

    [[AFNetworkTool shareManager]postJSONWithUrl:JW_Clockstatus parmas:dict successData:^(id json) {
        if ([json[@"message"]isEqualToString:@"success"]) {
            self.arrStatuses = json[@"clockstatuses"];
            NSArray *arr = json[@"list"];
            self.counts = 0;
            for (NSDictionary * dic in arr) {
                self.model = [PlanModel yy_modelWithJSON:dic];
                if (self.model.list.count > 0) {
                    self.counts += 1;
                    [self.dataArray addObject:self.model];
                    for (ContentModel *content in self.model.list) {
                        [self.contentArray addObject:content];
                    }
                }
            }
            /**判断是否有数据
             */
            if (self.counts > 0) {
                NSLog(@">>>you");
                [self.noDataView removeFromSuperview];
            }else{
                NSLog(@">>>wu");
                [self.tableView removeFromSuperview];
                [self.view addSubview:self.noDataView];
            }
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf.tableView reloadData];
        });
    } failure:^(NSError *error) {
        [Utility showFail:@"网络连接出错..."];
        NSLog(@"error%@",error);
    }];
}

/**
 处理打卡
 */
-(void)JwSetcard{
    if (self.arrStatuses.count > 0) {
        NSDictionary *dict = @{
                               @"jwid" : JWid,
                               @"jwpassword" : JWpassword,
                               @"cid" : self.arrStatuses[0][@"id"],
                               @"homework" : self.homework,
                               @"jwstate" :self.jwstate
                               };
        [[AFNetworkTool shareManager]postJSONWithUrl:JW_Setcard parmas:dict successData:^(id json) {
            [Utility showSuccess:@"成功!"];
            [self.navigationController popViewControllerAnimated:YES];
        } failure:^(NSError *error) {
            [Utility showFail:@"网络出错.."];
        }];
    }else{
        [Utility showFail:@"学生还未打卡!"];
    }

}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return  self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    self.model = self.dataArray[section];
    return  self.model.list.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ClockTableCell *cell=[ClockTableCell cellWithTableView:tableView withReuseIdentifier:@"ClockTableCellID"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.clockBtn setUserInteractionEnabled:NO];
    self.model = self.dataArray[indexPath.section];
    cell.contentModel= self.model.list[indexPath.row];
    /**判断是否已经打卡
     */
    if (self.arrStatuses.count > 0 ) {
        NSString *text = self.arrStatuses[0][@"content"];
        NSArray *p = [text componentsSeparatedByString:@","];
        for (int i = 0; i<p.count; i++) {
            if ([self.model.list[indexPath.row].contentID  isEqualToString:p[i]]) {
                [cell.clockBtn setImage:[UIImage imageNamed:@"stu-ka"] forState:UIControlStateNormal];
            }
        }
    }else{
    }
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    YHClockhead *head=[[YHClockhead alloc]init];
    self.model = self.dataArray[section];
    head.headcoursename.text=self.model.coursename;
    return head;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section == self.dataArray.count-1) {
        JWClockBottomView *foot = [[JWClockBottomView alloc] initWithReuseIdentifier:@"foot"];
        foot.jwDelegate = self;
        foot.contentModel = self.model.list[0];
        if (self.arrStatuses.count > 0) {
            NSString *version = [NSString stringWithFormat:@"%@",self.arrStatuses[0][@"version"]];
            if ([version isEqualToString:@""]) {
                version = @"无";
            }
            foot.text.text = version;
            
            if ([self.arrStatuses[0][@"homework"] isEqualToString:@"0"]) {
                [foot.clickBtn setImage:[UIImage imageNamed:@"stu-ka1"] forState:UIControlStateNormal];
            }else{
                [foot.clickBtn setImage:[UIImage imageNamed:@"stu-ka"] forState:UIControlStateNormal];
            }
            self.homework = self.arrStatuses[0][@"homework"];
        }
        return foot;
        
    }else{
        return nil;
    }
    
}

-(void)CheckBtn:(UIButton *)sender{
    self.jwstate = @"1";
    [self JwSetcard];
    
}
-(void)ZyBtn:(UIButton *)sender withModel:(ContentModel *)model{
    if (model.isChecked) {
        self.homework = @"1";
    }else{
        self.homework = @"0";
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == self.dataArray.count-1) {
        return 160;
    }else{
        return CGFLOAT_MIN;
    }
}

-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
    }
    return _tableView;
}

-(UIView *)noDataView{
    
    if (!_noDataView) {
        _noDataView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height-48)];
        _noDataView.backgroundColor = [UIColor whiteColor];
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.image = [UIImage imageNamed:@"NoData"];
        [_noDataView addSubview:imageView];
        
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(_noDataView);
            make.top.mas_equalTo(100);
            make.width.mas_equalTo(100);
            make.height.mas_equalTo(100);
        }];
        UILabel * la = [[UILabel alloc]init];
        la.text = @"亲,暂时没有打卡信息!";
        la.textColor = [UIColor darkGrayColor];
        la.textAlignment = NSTextAlignmentCenter;
        [_noDataView addSubview:la];
        [la mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(_noDataView);
            [make.top.mas_equalTo(imageView.mas_bottom) setOffset:10];
            make.width.mas_equalTo(200);
        }];
    }
    return _noDataView;
}

- (NSMutableArray *)dataArray {
    
    if(!_dataArray){
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (NSMutableArray *)contentArray {
    
    if(!_contentArray){
        _contentArray = [NSMutableArray array];
    }
    return _contentArray;
}
- (NSMutableArray *)contentIDArray {
    
    if(!_contentIDArray){
        _contentIDArray = [NSMutableArray array];
    }
    return _contentIDArray;
}
@end
